#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "str.h"
#include "list.h"
#include "config.h"
struct mr_conf_attr_s {
		char * key;
		char * val;
	  	mr_conf_attr_p next;
};
struct mr_conf_sec_s {
	char *section;
	mr_conf_attr_p pattr;
	mr_conf_sec_p next;
};
struct mr_conf_s {
	FILE *fp;
	mr_conf_sec_p psection;
	char *filename;
	
};
int mr_conf_section_new(mr_conf_sec_p * psec){

	*(psec) = (mr_conf_sec_p)malloc(sizeof(mr_conf_sec_t));
	memset(*(psec), 0, sizeof(mr_conf_sec_t));
	return 0;
}
int mr_conf_attr_new (mr_conf_attr_p * pattr){
	
	*(pattr) = (mr_conf_attr_p)malloc(sizeof(mr_conf_attr_t));
	memset(*(pattr),0,sizeof(mr_conf_attr_t));
	return 0;
}

int mr_conf_section_add(mr_conf_sec_p *psec,char * section){
	
	mr_conf_sec_p mysec;
	mr_conf_section_new(&mysec);
	mysec->section =strdup(section);
	mysec->next = (*psec);
	(*psec)=mysec;
	return 0;
}
int mr_conf_add_attr_to_sec(mr_conf_sec_p * psec,char *key ,char * val){
	mr_conf_attr_p myattr=NULL;
	mr_conf_attr_new(&myattr);
	myattr->key =strdup(key);
	myattr->val=strdup(val);
	myattr->next =(*psec)->pattr;
	(*psec)->pattr=myattr;
	return 0;
}

int mr_conf_parse(mr_conf_p * pconf){
	 char	* 	section;     
	 char	* 	pl;
	 char 	*	key;
	 char	*	comment;
	 char	* 	val;     
	 int 		lineno = 0;      
	 char		line[Max_Line_Length];
	mr_conf_sec_p psec = NULL;
	
	while(fgets(line,Max_Line_Length,(*pconf)->fp)!=NULL){
		lineno++;
		line[strlen(line)-1] ='\0';
		section=strchr(line,'[');
		if(section){
			*section++='\0';
			pl =strchr(section,']');
			if(pl){
				*pl--='\0';
				section=strip(section);
				mr_conf_section_add(&psec,section);
			}else{
				printf("Error in closing section at config file");
				return -1;
			}
		}else{
			// get key and values 
			val =strchr(line,'=');
			if(val)	{
				*val++='\0';
		                comment =strchr(val,'#');
				if(comment)
				{
				  *comment++='\0';
				   val=strip(val);
					key=strip(line);
				}else
				{
				val=strip(val);
				key=strip(line);
				}
			mr_conf_add_attr_to_sec(&psec,key,val);
			}
		}
			}
		(*pconf)->psection=psec;
	return 0;
}
int mr_conf_load(mr_conf_p * pconf,char* filename){
	mr_conf_p tconf;
	tconf=(mr_conf_p)malloc(sizeof(mr_conf_t));
	memset(tconf,0,sizeof(mr_conf_t));	
	tconf->filename =strdup(filename);			
	tconf->fp =fopen(tconf->filename,"rw+");
	if(tconf->fp ==NULL)
	{
	printf("Can't open conf File\n");
	return -1;
	}
	if(mr_conf_parse(&tconf))
	{
	printf("eror parsinf config file\n");
	return -1;	
	}
	*pconf=tconf;
	return 0;
}
int mr_conf_get(mr_conf_p pconf,char *psection,char *pkey,char **pvalue ){
	
	mr_conf_sec_p tmp;
	mr_conf_attr_p tattr;
	for(tmp=pconf->psection;tmp!=NULL  ;tmp=tmp->next)
	{
		if(strcmp(tmp->section,psection)==0)
		{
			for (tattr= tmp->pattr;tattr!=NULL;tattr=tattr->next)
			{
				if(strcmp(tattr->key,pkey)==0)
				{
					(*pvalue) = tattr->val;
					return 0;

				}
			}
		}
	}
return -1;
}

int mr_conf_set(mr_conf_p pconf ,char* psection,char* pkey,char* pvalue){
	return 0;
}
int mr_conf_get_sec_attrb(mr_conf_p pconf,char *psec,mr_conf_attr_p ** pattr){
	return 0;
}
int mr_conf_save(mr_conf_p pconf){
	return 0;
}
void mr_attr_free(mr_conf_attr_p pattr){
	mr_conf_attr_p nextattr;
		do {
			nextattr = pattr->next;
			free(pattr->key);
			if( pattr->val )
				free(pattr->val);
			free(pattr);
		} while( (pattr = nextattr) );
}
void mr_sec_free(mr_conf_sec_p psec){

	mr_conf_sec_p nextsec;
	do {
		nextsec = psec->next;
		free(psec->section);
		mr_attr_free( psec->pattr);
		free(psec);
	} while( (psec = nextsec) );
}
void mr_conf_free(mr_conf_p pconf) {

	mr_sec_free( pconf->psection);
	free(pconf->filename);
	fclose(pconf->fp);
	free(pconf);
}
